export const useConfirm = () => {
  const confirmDialog = ref({
    show: false,
    title: '',
    message: '',
    confirmText: '确定',
    cancelText: '取消',
    type: 'info', // 'info', 'warning', 'danger'
    resolve: null,
    reject: null
  })

  const showConfirm = (
    message: string,
    title: string = '确认操作',
    options: {
      confirmText?: string
      cancelText?: string
      type?: 'info' | 'warning' | 'danger'
    } = {}
  ): Promise<boolean> => {
    return new Promise((resolve, reject) => {
      confirmDialog.value = {
        show: true,
        title,
        message,
        confirmText: options.confirmText || '确定',
        cancelText: options.cancelText || '取消',
        type: options.type || 'info',
        resolve,
        reject
      }
    })
  }

  const confirm = () => {
    confirmDialog.value.resolve?.(true)
    hideConfirm()
  }

  const cancel = () => {
    confirmDialog.value.resolve?.(false)
    hideConfirm()
  }

  const hideConfirm = () => {
    confirmDialog.value.show = false
    confirmDialog.value.resolve = null
    confirmDialog.value.reject = null
  }

  return {
    confirmDialog: readonly(confirmDialog),
    showConfirm,
    confirm,
    cancel,
    hideConfirm
  }
}